package com.example.blogsystem.controller;

import com.example.blogsystem.mapper.BlogInfoMapper;
import com.example.blogsystem.model.BlogInfo;
import com.example.blogsystem.model.Result;
import com.example.blogsystem.service.BlogInfoService;
import com.example.blogsystem.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/blog")
public class BlogInfoController {

    @Autowired
    private BlogInfoService blogInfoService;
    @RequestMapping("/getList")
    public List<BlogInfo> getList() {
        return blogInfoService.getBlogList();
    }

    @RequestMapping("/getBlogContent")
    public Result getBlogContent(Integer blogId,HttpServletRequest request) {
        BlogInfo blogInfo =  blogInfoService.getBlog(blogId);
        if(blogInfo == null && !StringUtils.hasLength(blogInfo.getContent())) {
            return Result.fail("文章内容获取失败","");
        }
        String jwt = JwtUtils.getJwt(request);
        Integer userId = JwtUtils.getUserId(jwt);
        if(userId.equals(blogInfo.getUserId())) {
            blogInfo.setIsLoginUser(1);
        }
        return Result.success(blogInfo);
    }

    @RequestMapping("/addBlog")
    public Result addBlog(BlogInfo blogInfo, HttpServletRequest request) {
        if(blogInfo == null || !StringUtils.hasLength(blogInfo.getContent())) {
            return Result.fail("用户输入错误","");
        }
        String jwt = request.getHeader("user_token");
        Integer userId = JwtUtils.getUserId(jwt);
        blogInfo.setUserId(userId);
        Integer integer = blogInfoService.addBlogInfo(blogInfo);
        if(integer != null && integer > 0) {
            return Result.success(true);
        }
        return Result.fail("插入失败","");
    }

    @RequestMapping("/updateBlog")
    public Result updateBlog(BlogInfo blogInfo) {
        if(blogInfo.getId() == null || blogInfo.getId() < 0) {
            return Result.fail("blogId不合法",false);
        }
        Integer ret = blogInfoService.update(blogInfo);
        if(ret > 0) {
            return Result.success(true);
        }
        return Result.fail("更新失败",false);
    }

    @RequestMapping("/deleteBlog")
    public Result deleteBlog(Integer blogId) {
        if(blogId == null || blogId < 0) {
            return Result.fail("blogId不合法",false);
        }
        Integer ret = blogInfoService.delete(blogId);
        if(ret > 0) {
            return Result.success(true);
        }
        return Result.fail("删除失败",false);
    }

    @RequestMapping("count")
    public Result count() {
        Integer integer = blogInfoService.countBlogInfo();
        if(integer != null && integer > 0) {
            return Result.success(integer);
        }
        return Result.fail("获取失败","");
    }
}
